package com.android.questions.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.view.View;

import androidx.viewbinding.ViewBinding;

import com.android.base.ui.base.VBBaseActivity;
import com.android.base.util.ActivityClickUtil;
import com.android.base.util.DQLog;
import com.android.questions.R;
import com.android.questions.databinding.ActivityPushRecordBinding;
import com.android.questions.ui.adapter.PushRecordAdapter;
import com.hx.currency.HXCurrencySDK;
import com.hx.currency.data.qa.api.QAPushAppRecordResp;
import com.hx.currency.data.qa.model.QAPushAppRecord;
import com.qq.e.o.data.HttpUtil;
import com.qq.e.o.utils.JsonUtil;

import java.util.List;

public class PushRecordActivity extends VBBaseActivity implements View.OnClickListener {

    private ActivityPushRecordBinding pushRecordBinding;
    private PushRecordAdapter recordAdapter;

    public static void startActivity(Activity activity) {
        Intent intent = new Intent(activity, PushRecordActivity.class);
        activity.startActivity(intent);
    }

    @Override
    protected ViewBinding getViewBinding() {
        setTitleBackground(BG_WHITE);
        return pushRecordBinding = ActivityPushRecordBinding.inflate(getLayoutInflater());
    }

    @Override
    public void initView() {
        super.initView();
        ActivityClickUtil.getInstance().clear();
        pushRecordBinding.tvTitle.setText("下载记录");

        recordAdapter = new PushRecordAdapter();
        pushRecordBinding.rvRecord.setAdapter(recordAdapter);

        getRecordData();
    }

    private void getRecordData() {
        HXCurrencySDK.getInstance().getQaPushAppRecord(new HttpUtil.HttpUtilCallback() {
            @Override
            public void onSuccess(int i, String s) {
                DQLog.e("互推下载记录：" + s);
                QAPushAppRecordResp qaPushAppRecordResp = JsonUtil.parseObject(s, QAPushAppRecordResp.class);
                if (qaPushAppRecordResp != null && qaPushAppRecordResp.getErrorCode() == 0) {
                    updateUI(qaPushAppRecordResp.getRvl());
                } else {
                    updateUI(null);
                }
            }

            @Override
            public void onFailed(int i, Throwable throwable) {
                DQLog.e("互推下载记录失败：" + throwable.getMessage());
                updateUI(null);
            }
        });
    }

    private void updateUI(List<QAPushAppRecord> list) {
        if (list != null && list.size() > 0) {
            recordAdapter.setList(list);
            recordAdapter.notifyDataSetChanged();
            pushRecordBinding.rvRecord.setVisibility(View.VISIBLE);
            pushRecordBinding.tvEmpty.setVisibility(View.GONE);
        } else {
            pushRecordBinding.rvRecord.setVisibility(View.GONE);
            pushRecordBinding.tvEmpty.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void initLister() {
        super.initLister();
        pushRecordBinding.imgBack.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (ActivityClickUtil.getInstance().isFastClick()) {
            //防止用户快速点击
            return;
        }
        if (v.getId() == R.id.imgBack) {
            finish();
        }
    }
}